﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies.Takenlijst_windows
{
    public partial class Edittaak2 : UserControl
    {
        private int projectID;
        private string taaknaam;
        private int taakID;
        private string nieuwenaam;

        MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");

        public Edittaak2()
        {
            InitializeComponent();
        }

        public Edittaak2(int projectnummer, string taskname)
        {
            projectID = projectnummer;
            taaknaam = taskname;
            InitializeComponent();
            getTaskID();
        }

        private void getTaskID()
        {
            MySqlCommand idGet = new MySqlCommand("Select TaakID From taken where ProjectID='"+projectID+"'AND Taak='"+taaknaam+"'", grabConnection);
            grabConnection.Open();
            MySqlDataReader reader = idGet.ExecuteReader();

            if (reader.Read() != false)
            {
                if (reader.IsDBNull(0) == true)
                {
                    grabConnection.Close();
                    reader.Dispose();
                    idGet.Dispose();
                }
                else
                {
                    taakID = Int32.Parse(reader["TaakID"].ToString());
                    reader.Close();
                    grabConnection.Close();
                }
            }
        }

        public void saveTask()
        {
            MySqlCommand command = new MySqlCommand("Update taken SET Taak=@nieuwenaam WHERE TaakID=@taakid", grabConnection);
            command.Parameters.Add(new MySqlParameter("nieuwenaam", nieuwenaam));
            command.Parameters.Add(new MySqlParameter("taakid", taakID));
            
            try
            {
                grabConnection.Open();
                command.ExecuteNonQuery();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
                this.Controls.Clear();
                this.Controls.Add(new Takenlijst(projectID));
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {
            label3.Text = taaknaam;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            nieuwenaam = textBox1.Text;

            if (nieuwenaam != "")
            {
                saveTask();
            }
            else
            {
                MessageBox.Show("Niet alle velden zijn ingevuld!");
            }
        }
    }
}
